/**   
* @Title: WebNrsPersonController.java 
* @Package com.txby.platformApi.component.web 
* @Description: 护工管理
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
* @version V1.0   
*/
package com.txby.platformApi.component.web;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformApi.component.service.WebNrsPersonService;
import com.txby.platformCore.entity.shop.NrsPerson;

/** 
* @ClassName: WebNrsPersonController 
* @Description: 护工管理 
* @author Administrator
* @date 2017年11月2日 下午4:07:49 
*  
*/
@RestController
@RequestMapping("/web/nrsperson")
public class WebNrsPersonController extends CommonController {

	@Resource
	public WebNrsPersonService webNrsPersonService;
	
	@RequestMapping("/list")
	@RequiresPermissions("sp:goods:info")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="select * from t_txby_sp_nrsPerson t where status=0 " ;
		String name=params.get("name")==null?"":params.get("name").toString();
		if(!ChkUtil.isEmptyAllObject(name)){
			if (name.contains("%") || name.contains("_")) {
				name = name.trim().replace("%", "\\%").replace("_", "\\_");
				name = "%" + name + "%";
				sql += " and bg.name like #{pojo.name} escape '\\'";
				params.put("name", "%" + name.trim() + "%");
			} else {
				name = "%" + name.trim() + "%";
				sql += " and bg.name like #{pojo.name} ";
				params.put("name", "%" + name.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 查询信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sp:goods:info")
	public R info(@PathVariable("id") String id){
		String sql="SELECT t.*  FROM  "+this.mBaseDao.tableName(NrsPerson.class)+" t WHERE  t.id='"+id+"'" ;
		Map goods =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("goods", goods);
	}
	
	/**
	 * 保存库存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sp:goods:save")
	public R save(@RequestBody NrsPerson goods){
		try {
			ValidatorUtils.validateEntity(goods);
			String sql="select * from "+this.mBaseDao.tableName(NrsPerson.class)+" where ICNO='"+goods.getICNO()+"'";
			NrsPerson old=(NrsPerson) this.mBaseDao.findBySqlUniqueObject(sql, NrsPerson.class, null);
			 if(old!=null){
				 return R.ok().error("该护工已存在");
			 }
			 goods.setCreater(getUserId().toString());
			 goods.setUpdater(getUserId().toString());
			goods.setCreateTime(new Date());
			goods.setCreateTime(new Date());
			goods.setStatus(0);
			webNrsPersonService.saveNrsPerson(goods);
		} catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sp:goods:update")
	public R update(@RequestBody  NrsPerson goods){
		try {
			ValidatorUtils.validateEntity(goods, AddGroup.class);
			String sql="select * from "+this.mBaseDao.tableName(NrsPerson.class)+" where ICNO ="+goods.getICNO()+" and id_!="+goods.getId_()+" limit 1";
			NrsPerson old=(NrsPerson) this.mBaseDao.findBySqlUniqueObject(sql, NrsPerson.class, null);
			if(old!=null){
				return R.ok().error("该护工已存在");
			}else{
				old=(NrsPerson) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(NrsPerson.class)+" where   id_="+goods.getId_(), NrsPerson.class, null);
				if(old!=null){
					this.mBaseDao.updateObject(goods);
				}
			}
			
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sp:goods:delete")
	public R delete(@RequestBody String[] ids){
		 for(String id:ids){
		    	this.mBaseDao.executeSql(" update  "+this.mBaseDao.tableName(NrsPerson.class)+" set status=1 where id='"+id+"'", null);
		  }
		return R.ok();
	}
}
